package view;



import java.awt.Component;
import java.awt.Dialog;
import java.awt.FileDialog;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.JViewport;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Utilities;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.UndoManager;

import model.Ferramentas;

import utils.TextLineNumber;
import controller.AjudaController;
import controller.ArquivoController;
import controller.EditarController;
import controller.FerramentasController;
import controller.FormatarController;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author 071211281
 */
public class EditorView extends javax.swing.JFrame {

    /**
     * Creates new form EditorView
     */
    public EditorView() {
        initComponents();
        
       
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenu7 = new javax.swing.JMenu();
        jMenu8 = new javax.swing.JMenu();
        jMenu9 = new javax.swing.JMenu();
        jMenu11 = new javax.swing.JMenu();
        jMenuItem22 = new javax.swing.JMenuItem();
        jToolBar2 = new javax.swing.JToolBar();
        jLabel1 = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jTabbedPane4 = new javax.swing.JTabbedPane();
        jToolBar1 = new javax.swing.JToolBar();
        bt_novo = new javax.swing.JButton();
        bt_abrir = new javax.swing.JButton();
        bt_salvar = new javax.swing.JButton();
        bt_salvar_como = new javax.swing.JButton();
        bt_salvar_todos = new javax.swing.JButton();
        bt_imprimir = new javax.swing.JButton();
        bt_fechar = new javax.swing.JButton();
        bt_fechar_todos = new javax.swing.JButton();
        bt_propriedades = new javax.swing.JButton();
        jToolBar3 = new javax.swing.JToolBar();
        bt_selecionar = new javax.swing.JButton();
        bt_selecionar_tudo = new javax.swing.JButton();
        bt_copiar = new javax.swing.JButton();
        bt_colar = new javax.swing.JButton();
        bt_recortar = new javax.swing.JButton();
        bt_desfazer = new javax.swing.JButton();
        bt_refazer = new javax.swing.JButton();
        jToolBar4 = new javax.swing.JToolBar();
        bt_justificado = new javax.swing.JButton();
        bt_centralizar = new javax.swing.JButton();
        bt_direita = new javax.swing.JButton();
        bt_esquerda = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        bt_cabecalho = new javax.swing.JButton();
        bt_rodape = new javax.swing.JButton();
        jSeparator5 = new javax.swing.JToolBar.Separator();
        bt_negrito = new javax.swing.JButton();
        bt_italico = new javax.swing.JButton();
        bt_sublinhado = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        sel_fonte = new javax.swing.JComboBox();
        sel_tamanho_fonte = new javax.swing.JComboBox();
        sel_cor_fonte = new javax.swing.JComboBox();
        sel_esp_linha = new javax.swing.JComboBox();
        jToolBar6 = new javax.swing.JToolBar();
        bt_sobre_sistema = new javax.swing.JButton();
        bt_obter_ajuda = new javax.swing.JButton();
        jToolBar5 = new javax.swing.JToolBar();
        bt_busca_padrao = new javax.swing.JButton();
        bt_busca_binaria = new javax.swing.JButton();
        jSeparator4 = new javax.swing.JToolBar.Separator();
        bt_substituir = new javax.swing.JButton();
        //bt_destacar_sintaxe = new javax.swing.JButton();
        jSeparator6 = new javax.swing.JToolBar.Separator();
        bt_corretor_ortografico = new javax.swing.JButton();
        bt_ordernar_alfabeticamente = new javax.swing.JButton();
        bt_num_linha = new javax.swing.JButton();

        jMenu7.setText("jMenu7");

        jMenu8.setText("jMenu8");

        jMenu9.setText("jMenu9");

        jMenu11.setText("jMenu11");

        jMenuItem22.setText("jMenuItem22");

        jToolBar2.setRollover(true);

        jLabel1.setText("jLabel1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        
        //Retirei todas as inicializações da primeira aba e, no lugar, chamo o metodo que cria nova aba.
        bt_novoActionPerformed(null);
        
        jToolBar1.setRollover(true);

        bt_novo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/novo.png"))); // NOI18N
        bt_novo.setToolTipText("Novo");
        bt_novo.setFocusable(false);
        bt_novo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_novo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_novo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_novoActionPerformed(evt);
            }
        });
        jToolBar1.add(bt_novo);

        bt_abrir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/abrir.png"))); // NOI18N
        bt_abrir.setToolTipText("Abrir");
        bt_abrir.setFocusable(false);
        bt_abrir.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_abrir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_abrir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_abrirActionPerformed(evt);
            }
        });
        jToolBar1.add(bt_abrir);

        bt_salvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/salvar.png"))); // NOI18N
        bt_salvar.setToolTipText("Salvar");
        bt_salvar.setFocusable(false);
        bt_salvar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_salvar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_salvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_salvarActionPerformed(evt);
            }
        });
        jToolBar1.add(bt_salvar);

        bt_salvar_como.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/salvarComo.png"))); // NOI18N
        bt_salvar_como.setToolTipText("Salvar Como");
        bt_salvar_como.setFocusable(false);
        bt_salvar_como.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_salvar_como.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_salvar_como.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_salvar_comoActionPerformed(evt);
            }
        });
        jToolBar1.add(bt_salvar_como);

        bt_salvar_todos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/salvarTodos.png"))); // NOI18N
        bt_salvar_todos.setToolTipText("Salvar Todos");
        bt_salvar_todos.setFocusable(false);
        bt_salvar_todos.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_salvar_todos.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_salvar_todos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_salvar_todosActionPerformed(evt);
            }
        });
        jToolBar1.add(bt_salvar_todos);

        bt_imprimir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/imprimir.png"))); // NOI18N
        bt_imprimir.setToolTipText("Imprimir");
        bt_imprimir.setFocusable(false);
        bt_imprimir.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_imprimir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_imprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_imprimirActionPerformed(evt);
            }
        });
        
        jToolBar1.add(bt_imprimir);

        bt_fechar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/fechar.png"))); // NOI18N
        bt_fechar.setToolTipText("Fechar");
        bt_fechar.setFocusable(false);
        bt_fechar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_fechar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_fechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_fecharActionPerformed(evt);
            }
        });
        jToolBar1.add(bt_fechar);

        bt_fechar_todos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/fecharTodos.png"))); // NOI18N
        bt_fechar_todos.setToolTipText("Fechar Todos");
        bt_fechar_todos.setFocusable(false);
        bt_fechar_todos.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_fechar_todos.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_fechar_todos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_fechar_todosActionPerformed(evt);
            }
        });
        jToolBar1.add(bt_fechar_todos);

        bt_propriedades.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/configuracao.png"))); // NOI18N
        bt_propriedades.setToolTipText("Propriedades");
        bt_propriedades.setFocusable(false);
        bt_propriedades.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_propriedades.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_propriedades.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_propriedadesActionPerformed(evt);
            }
        });
        jToolBar1.add(bt_propriedades);

        jTabbedPane4.addTab("Arquivo", jToolBar1);

        jToolBar3.setRollover(true);

        bt_selecionar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/selecionar.png"))); // NOI18N
        bt_selecionar.setToolTipText("Selecionar");
        bt_selecionar.setFocusable(false);
        bt_selecionar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_selecionar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_selecionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_selecionarActionPerformed(evt);
            }
        });
        jToolBar3.add(bt_selecionar);

        bt_selecionar_tudo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/selecionarTodos.png"))); // NOI18N
        bt_selecionar_tudo.setToolTipText("Selecionar Tudo");
        bt_selecionar_tudo.setFocusable(false);
        bt_selecionar_tudo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_selecionar_tudo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_selecionar_tudo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_selecionar_tudoActionPerformed(evt);
            }
        });
        jToolBar3.add(bt_selecionar_tudo);

        bt_copiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/copiar.png"))); // NOI18N
        bt_copiar.setToolTipText("Copiar");
        bt_copiar.setFocusable(false);
        bt_copiar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_copiar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_copiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_copiarActionPerformed(evt);
            }
        });
        jToolBar3.add(bt_copiar);

        bt_colar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/colar.png"))); // NOI18N
        bt_colar.setToolTipText("Colar");
        bt_colar.setFocusable(false);
        bt_colar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_colar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_colar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {      	
        		bt_colarActionPerformed(evt);
            }
        });
        jToolBar3.add(bt_colar);

        bt_recortar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/recortar.png"))); // NOI18N
        bt_recortar.setToolTipText("Recortar");
        bt_recortar.setFocusable(false);
        bt_recortar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_recortar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_recortar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_recortarActionPerformed(evt);
            }
        });
        jToolBar3.add(bt_recortar);

        bt_desfazer.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/desfazer.png"))); // NOI18N
        bt_desfazer.setToolTipText("Desfazer");
        bt_desfazer.setFocusable(false);
        bt_desfazer.setEnabled(false);
        bt_desfazer.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_desfazer.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_desfazer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_desfazerActionPerformed(evt);
            }
        });
        jToolBar3.add(bt_desfazer);

        bt_refazer.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/refazer.png"))); // NOI18N
        bt_refazer.setToolTipText("Refazer");
        bt_refazer.setFocusable(false);
        bt_refazer.setEnabled(false);
        bt_refazer.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_refazer.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_refazer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_refazerActionPerformed(evt);
            }
        });
        jToolBar3.add(bt_refazer);

        jTabbedPane4.addTab("Editar", jToolBar3);

        jToolBar4.setRollover(true);

        bt_justificado.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/justificado.png"))); // NOI18N
        bt_justificado.setToolTipText("Justificar");
        bt_justificado.setFocusable(false);
        bt_justificado.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_justificado.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_justificado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_justificadoActionPerformed(evt);
            }
        });
        jToolBar4.add(bt_justificado);

        bt_centralizar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/centralizado.png"))); // NOI18N
        bt_centralizar.setToolTipText("Centralizar");
        bt_centralizar.setFocusable(false);
        bt_centralizar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_centralizar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_centralizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_centralizarActionPerformed(evt);
            }
        });
        jToolBar4.add(bt_centralizar);

        bt_direita.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/centralizadoDireita.png"))); // NOI18N
        bt_direita.setToolTipText("Direita");
        bt_direita.setFocusable(false);
        bt_direita.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_direita.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_direita.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_direitaActionPerformed(evt);
            }
        });
        jToolBar4.add(bt_direita);

        bt_esquerda.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/centralizadoEsqueda.png"))); // NOI18N
        bt_esquerda.setToolTipText("Esquerda");
        bt_esquerda.setFocusable(false);
        bt_esquerda.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_esquerda.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_esquerda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_esquerdaActionPerformed(evt);
            }
        });
        jToolBar4.add(bt_esquerda);
        
        jToolBar4.add(jSeparator1);

        bt_cabecalho.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/cabecalho.png"))); // NOI18N
        bt_cabecalho.setToolTipText("Aumentar Recuo");
        bt_cabecalho.setFocusable(false);
        bt_cabecalho.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_cabecalho.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_cabecalho.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_cabecalhoActionPerformed(evt);
            }
        });
        jToolBar4.add(bt_cabecalho);

        bt_rodape.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/rodape.png"))); // NOI18N
        bt_rodape.setToolTipText("Diminuir Recuo");
        bt_rodape.setFocusable(false);
        bt_rodape.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_rodape.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_rodape.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_rodapeActionPerformed(evt);
            }
        });
        jToolBar4.add(bt_rodape);

        jToolBar4.add(jSeparator5);

        bt_negrito.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/negrito.png"))); // NOI18N
        bt_negrito.setToolTipText("Negrito");
        bt_negrito.setFocusable(false);
        bt_negrito.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_negrito.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_negrito.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_negritoActionPerformed(evt);
            }
        });
        jToolBar4.add(bt_negrito);

        bt_italico.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/italico.png"))); // NOI18N
        bt_italico.setToolTipText("Italico");
        bt_italico.setFocusable(false);
        bt_italico.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_italico.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_italico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_italicoActionPerformed(evt);
            }
        });
        jToolBar4.add(bt_italico);

        bt_sublinhado.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/subilinhado.png"))); // NOI18N
        bt_sublinhado.setToolTipText("Sublinhado");
        bt_sublinhado.setFocusable(false);
        bt_sublinhado.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_sublinhado.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_sublinhado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_sublinhadoActionPerformed(evt);
            }
        });
        jToolBar4.add(bt_sublinhado);

        sel_fonte.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Arial", "Comic Sans MS", "Times New Roman", "Verdana" }));
        sel_fonte.setToolTipText("Fonte");
        sel_fonte.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sel_fonteActionPerformed(evt);
            }
        });

        sel_tamanho_fonte.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "8", "12", "16", "20" }));
        sel_tamanho_fonte.setToolTipText("Tamanho");
        sel_tamanho_fonte.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sel_tamanho_fonteActionPerformed(evt);
            }
        });
        
        sel_cor_fonte.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Preto", "Azul", "Vermelho", "Verde" }));
        sel_cor_fonte.setToolTipText("Cor");
        sel_cor_fonte.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sel_cor_fonteActionPerformed(evt);
            }
        });
        
        sel_esp_linha.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0,0", "1,0", "1,15", "1,5", "2,0" }));
        sel_esp_linha.setToolTipText("Espaçamento de Linha");
        sel_esp_linha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sel_esp_linhaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(sel_fonte, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(sel_tamanho_fonte, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(sel_cor_fonte, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(sel_esp_linha, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(510, Short.MAX_VALUE))
        );
        /*
        .addComponent(sel_tamanho_fonte, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(348, Short.MAX_VALUE)
        */
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sel_fonte, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sel_tamanho_fonte, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sel_cor_fonte, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sel_esp_linha, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(36, Short.MAX_VALUE))
        );

        jTabbedPane1.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                updateButtons();
            }
        });

        sel_fonte.getAccessibleContext().setAccessibleName("");

        jToolBar4.add(jPanel1);

        jTabbedPane4.addTab("Formatar", jToolBar4);

        jToolBar6.setRollover(true);

        bt_sobre_sistema.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/sobreSiste.png"))); // NOI18N
        bt_sobre_sistema.setToolTipText("Sobre o Sistema");
        bt_sobre_sistema.setFocusable(false);
        bt_sobre_sistema.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_sobre_sistema.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_sobre_sistema.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_sobre_sistemaActionPerformed(evt);
            }
        });
        jToolBar6.add(bt_sobre_sistema);

        bt_obter_ajuda.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/ajuda.png"))); // NOI18N
        bt_obter_ajuda.setToolTipText("Obter Ajuda");
        bt_obter_ajuda.setFocusable(false);
        bt_obter_ajuda.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_obter_ajuda.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_obter_ajuda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_obter_ajudaActionPerformed(evt);
            }
        });
        jToolBar6.add(bt_obter_ajuda);

        jTabbedPane4.addTab("Ajuda", jToolBar6);

        jToolBar5.setRollover(true);

        bt_busca_padrao.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/buscaPadrao.png"))); // NOI18N
        bt_busca_padrao.setToolTipText("Busca Padr‹o");
        bt_busca_padrao.setFocusable(false);
        bt_busca_padrao.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_busca_padrao.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_busca_padrao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_busca_padraoActionPerformed(evt);
            }
        });
        jToolBar5.add(bt_busca_padrao);

        bt_busca_binaria.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/buscaBinaria.png"))); // NOI18N
        bt_busca_binaria.setToolTipText("Busca Bin‡ria");
        bt_busca_binaria.setFocusable(false);
        bt_busca_binaria.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_busca_binaria.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_busca_binaria.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_busca_binariaActionPerformed(evt);
            }
        });
        jToolBar5.add(bt_busca_binaria);
        
        jToolBar5.add(jSeparator4);

        bt_substituir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/substituir.png"))); // NOI18N
        bt_substituir.setToolTipText("Substituir");
        bt_substituir.setFocusable(false);
        bt_substituir.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_substituir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_substituir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_substituirActionPerformed(evt);
            }
        });
        jToolBar5.add(bt_substituir);

       /* bt_destacar_sintaxe.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/destacarSintaxe.png"))); // NOI18N
        bt_destacar_sintaxe.setToolTipText("Destacar Sintaxe");
        bt_destacar_sintaxe.setFocusable(false);
        bt_destacar_sintaxe.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_destacar_sintaxe.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_destacar_sintaxe.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_destacar_sintaxeActionPerformed(evt);
            }
        });
        jToolBar5.add(bt_destacar_sintaxe);
        */
        jToolBar5.add(jSeparator6);

        bt_corretor_ortografico.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/corretor.png"))); // NOI18N
        bt_corretor_ortografico.setToolTipText("Corretor Ortogr‡fico");
        bt_corretor_ortografico.setFocusable(false);
        bt_corretor_ortografico.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_corretor_ortografico.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_corretor_ortografico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_corretor_ortograficoActionPerformed(evt);
            }
        });
        jToolBar5.add(bt_corretor_ortografico);

        bt_ordernar_alfabeticamente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/ordenarAlfa.png"))); // NOI18N
        bt_ordernar_alfabeticamente.setToolTipText("Ordenar Alfabeticamente");
        bt_ordernar_alfabeticamente.setFocusable(false);
        bt_ordernar_alfabeticamente.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_ordernar_alfabeticamente.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_ordernar_alfabeticamente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_ordernar_alfabeticamenteActionPerformed(evt);
            }
        });
        jToolBar5.add(bt_ordernar_alfabeticamente);

        bt_num_linha.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/numeroLinha.png"))); // NOI18N
        bt_num_linha.setToolTipText("Nœmeros de Linhas");
        bt_num_linha.setFocusable(false);
        bt_num_linha.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_num_linha.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_num_linha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_num_linhaActionPerformed(evt);
            }
        });
        jToolBar5.add(bt_num_linha);

        jTabbedPane4.addTab("Ferramentas", jToolBar5);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane4)
            .addComponent(jTabbedPane1)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTabbedPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 388, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    
 int cont;
    private void bt_novoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_novoActionPerformed
        // TODO add your handling code here:
    	  
    	     fecharAbasUnico();	  
    	     
    	     JTextPane novoAba =arquivo.novo();
    	     novoScroll= new JScrollPane();
    	     
    	     UndoManager undoNovaAba = new UndoManager();
    	     undoList.add(undoNovaAba);
    	     
    	     novoAba.getDocument().addUndoableEditListener(
	             new UndoableEditListener() {
	               public void undoableEditHappened(UndoableEditEvent e) {
	                 undoList.get(jTabbedPane1.getSelectedIndex()).addEdit(e.getEdit());
	                 updateButtons();
	               }
	             }
	         );
    	     
    	     if(jTabbedPane1.getTabCount()==1){
    	    	 cont =2;
    	     }else if(jTabbedPane1.getTabCount()==0)
    	    	   cont = 1;
    	     else cont= jTabbedPane1.getTabCount()+1;
    	     
    	     novoScroll.setViewportView(novoAba);
    	     TextLineNumber tln = new TextLineNumber(novoAba);
    	    
    	     jTabbedPane1.addTab("tab"+cont,novoScroll);//Adicionando TextPane ao TabePane
    	     jTabbedPane1.setSelectedComponent(novoScroll);
    	     ((JScrollPane)jTabbedPane1.getSelectedComponent()).setRowHeaderView(tln);
    	     ((JScrollPane)jTabbedPane1.getSelectedComponent()).getRowHeader().setVisible(false);

    	     	
    	 
    	
    }//GEN-LAST:event_bt_novoActionPerformed
   
    

    private void bt_salvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_salvarActionPerformed
        // TODO add your handling code here:
    	
    	   //AbaSelec =(JTextPane)jTabbedPane1.getSelectedComponent();
    	
    	
    	    selecionarAba();
    	    
    	   
    	   
    if( jTabbedPane1.getSelectedComponent()!=null){
    	  
    	  
    	  if(AbaSelec.getName()==null){
    		 
    		  salvarArq = new FileDialog(new Dialog(this), "Salvando", FileDialog.SAVE);
              salvarArq.setVisible(true);
              
              if(salvarArq.getFile() !=null){
                jTabbedPane1.setTitleAt(num,salvarArq.getFile());
                arquivo.salvar(salvarArq.getDirectory()+salvarArq.getFile()+".txt",  AbaSelec.getText());
                AbaSelec.setName(salvarArq.getDirectory()+salvarArq.getFile()+".txt");
              }
  
    	  }else{
    		  
    		  
        	      arquivo.salvar(AbaSelec.getName(), AbaSelec.getText());
        	      JOptionPane.showMessageDialog(null, "Salvo com sucesso");
        		  
    	  }
    
    	  
    }
    	
    }//GEN-LAST:event_bt_salvarActionPerformed

    private void bt_abrirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_abrirActionPerformed
        // TODO add your handling code here:
    	
    	
    	 
   
    	FileDialog abrirArq = new FileDialog(new Dialog(this), "Selecione o arquivo", FileDialog.LOAD);//Instanciando Caixa de dialogo para salvar
    	abrirArq.setVisible(true);//Tordando caixa de dialogo visivel
    	String caminho = abrirArq.getDirectory();//Pegando diretorio onde esta o arquivo
    	String nomeArq=abrirArq.getFile();//Pegando o nome do arquivo selecionado

    	if(nomeArq !=null){//Se nome do arquivo for diferente de null faça
    	
    		
    		   JTextPane abrirtext = new JTextPane();
    		   novoScroll= new JScrollPane();
    		   cont++;

    		   
    		   abrirtext.setText(arquivo.abrir(caminho,nomeArq));//setando texto no TextPane ou TextArea
    		   abrirtext.setName(abrirArq.getDirectory()+abrirArq.getFile());
    		   
    		   UndoManager undoNovaAba = new UndoManager();
    		   undoList.add(undoNovaAba);
      	     
      	     	abrirtext.getDocument().addUndoableEditListener(
					new UndoableEditListener() {
					  public void undoableEditHappened(UndoableEditEvent e) {
					    undoList.get(jTabbedPane1.getSelectedIndex()).addEdit(e.getEdit());
					    updateButtons();
					  }
					}
     			);
      	     	
      	     	
      	       fecharAbasUnico();	
      	       
      	       
     
    		   novoScroll.setViewportView(abrirtext);
    		   jTabbedPane1.addTab(abrirArq.getFile(),novoScroll);//Setando o Scroll dentro do Tab
    		   jTabbedPane1.setSelectedComponent(novoScroll);
    		   
    		   TextLineNumber tln = new TextLineNumber(abrirtext);
    		   ((JScrollPane)jTabbedPane1.getSelectedComponent()).setRowHeaderView(tln);
    		   ((JScrollPane)jTabbedPane1.getSelectedComponent()).getRowHeader().setVisible(false);
    		   
    	}
    	
    	
    }//GEN-LAST:event_bt_abrirActionPerformed

    private void bt_salvar_comoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_salvar_comoActionPerformed

    	
    	 selecionarAba();
    	 
    	
    if( jTabbedPane1.getSelectedComponent()!=null){
    	
    	  if(AbaSelec.getName()!=null || AbaSelec.getName()==null){
     		 
    		  salvarArq = new FileDialog(new Dialog(this), "Salvar como", FileDialog.SAVE);
              salvarArq.setVisible(true);
              
              if(salvarArq.getFile() !=null){
                jTabbedPane1.setTitleAt(num,salvarArq.getFile());
                arquivo.salvar(salvarArq.getDirectory()+salvarArq.getFile()+".txt",  AbaSelec.getText());
                AbaSelec.setName(salvarArq.getDirectory()+salvarArq.getFile()+".txt");
              }
    	  }
    	 
    	}
    }//GEN-LAST:event_bt_salvar_comoActionPerformed

    private void bt_salvar_todosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_salvar_todosActionPerformed
        // TODO add your handling code here:
    	
    	int contGuia= jTabbedPane1.getTabCount();
    	int vez;
    	String nometab;
    	
   	
    	
    	
    	for (vez=0;vez<contGuia;vez++){
    		
    		selecionarAba2(vez);
    		
    		if(AbaSelec.getName()==null){
    	    	   
    	    	   int n=vez+1;
    	    	   nometab="tab"+n;
    	       }else
    	    	   nometab=AbaSelec.getName();
    		
    		JOptionPane.showMessageDialog(null, "Salvando:"+nometab);
    		
      	  if( AbaSelec.getName()==null){
      		 
    		  salvarArq = new FileDialog(new Dialog(this), "Salvar", FileDialog.SAVE);
    		  salvarArq.setVisible(true);
              
              if(salvarArq.getFile() !=null){
                jTabbedPane1.setTitleAt(vez,salvarArq.getFile());
                arquivo.salvar(salvarArq.getDirectory()+salvarArq.getFile()+".txt",  AbaSelec.getText());
                AbaSelec.setName(salvarArq.getDirectory()+salvarArq.getFile()+".txt");
              }else{
            	  
            	  arquivo.salvar(AbaSelec.getName(), AbaSelec.getText());
              }
            	  
    	  }
    		 		
   }
    	
    	
    	
    }//GEN-LAST:event_bt_salvar_todosActionPerformed

    private void bt_imprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_imprimirActionPerformed
        // TODO add your handling code here:
    	 
    	 selecionarAba();
    if( jTabbedPane1.getSelectedComponent()!=null)
    	 arquivo.imprimir(AbaSelec);
    	 
    	 
    }//GEN-LAST:event_bt_imprimirActionPerformed

 private void bt_fecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_fecharActionPerformed
        // TODO add your handling code here:
    	
	 
	  String nometab = null;
    	 selecionarAba();
    	   
    	 
    	 
    	if( jTabbedPane1.getSelectedComponent()!=null){
            nometab = jTabbedPane1.getTitleAt(jTabbedPane1.getSelectedIndex());
 	   
 	   
    	   int resposta=JOptionPane.showConfirmDialog(null, "Deseja salvar alteracoes em: "+ nometab+" ?");
    		
    	   if(resposta == JOptionPane.YES_OPTION){	
			      	   if(AbaSelec.getName()==null){
			      		 
			    		        salvarArq = new FileDialog(new Dialog(this), "Salvar", FileDialog.SAVE);
			    		       salvarArq.setVisible(true);
			              
				               if(salvarArq.getFile() !=null){
				                   jTabbedPane1.setTitleAt(num,salvarArq.getFile());
				                   arquivo.salvar(salvarArq.getDirectory()+salvarArq.getFile()+".txt",  AbaSelec.getText());
				                   AbaSelec.setName(salvarArq.getDirectory()+salvarArq.getFile()+".txt");
				               }
			    	     }else{
			    	    	 
			    	    	 arquivo.salvar(AbaSelec.getName(), AbaSelec.getText()); 
			    	     }
	      	   
    	    } else if(resposta == JOptionPane.CANCEL_OPTION){
	    	     	
	    	    	    return;
	        }
      	 
      	 
        if(num>=0){ 
    	  jTabbedPane1.removeTabAt(num);
    	
    	   arquivo.fechar();
       }
    }
    	
}//GEN-LAST:event_bt_fecharActionPerformed

private void bt_fechar_todosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_fechar_todosActionPerformed
        // TODO add your handling code here:
    	
    	int contGuia= jTabbedPane1.getTabCount();
    	int vez;
    	String nometab;
    	
    
    	if(contGuia==0)return;
    	
     for (vez=contGuia-1;vez>=0;vez--){
    
    		selecionarAba2(vez);
    
    		
    		 nometab = jTabbedPane1.getTitleAt(jTabbedPane1.getSelectedIndex());
    	   
    	   
    	int resposta=JOptionPane.showConfirmDialog(null, "Deseja salvar alteracoes em "+ nometab+" ?");
    		
    	if(resposta == JOptionPane.YES_OPTION){	
	      	   if(AbaSelec.getName()==null){
	      		 
	    		  salvarArq = new FileDialog(new Dialog(this), "Salvar", FileDialog.SAVE);
	    		  salvarArq.setVisible(true);
	              
		              if(salvarArq.getFile() !=null){
		                jTabbedPane1.setTitleAt(vez,salvarArq.getFile());
		                arquivo.salvar(salvarArq.getDirectory()+salvarArq.getFile()+".txt",  AbaSelec.getText());
		                AbaSelec.setName(salvarArq.getDirectory()+salvarArq.getFile()+".txt");
		              }
	    	     }else{
	    	    	 
	    	    	 arquivo.salvar(AbaSelec.getName(), AbaSelec.getText());
	    	    	 
	    	    	 
	    	      }
	    	    	 
	      	   
	      	   
    	  } else if(resposta == JOptionPane.CANCEL_OPTION){
    	
    	    return;
          }
    	
     
    	   jTabbedPane1.removeTabAt(vez);
    	
     } 
    	
    
    	
    	
    }//GEN-LAST:event_bt_fechar_todosActionPerformed
    
   

    private void bt_propriedadesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_propriedadesActionPerformed
        // TODO add your handling code here:
    	
    	selecionarAba();
    	if( jTabbedPane1.getSelectedComponent()!=null){	
        	arquivo.propriedades(AbaSelec);
    	}
    	
    	
    }//GEN-LAST:event_bt_propriedadesActionPerformed

    private void bt_selecionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_selecionarActionPerformed
    	selecionarAba();
    	try{
    		AbaSelec.select(Utilities.getRowStart(AbaSelec, AbaSelec.getCaretPosition()), Utilities.getRowEnd(AbaSelec, AbaSelec.getCaretPosition()));
    	}catch(BadLocationException ble){}
        // TODO add your handling code here:
    }//GEN-LAST:event_bt_selecionarActionPerformed

    
 
    
    private void bt_selecionar_tudoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_selecionar_tudoActionPerformed
        
    
    	 selecionarAba();
    	 AbaSelec.selectAll();
    }//GEN-LAST:event_bt_selecionar_tudoActionPerformed

    private void bt_copiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_copiarActionPerformed

    	 selecionarAba();
    	
    	if( AbaSelec.getSelectedText() != null){
    		editar.copiar( AbaSelec.getSelectedText());
    	}
    }//GEN-LAST:event_bt_copiarActionPerformed

    private void bt_colarActionPerformed(java.awt.event.ActionEvent evt)
    {//GEN-FIRST:event_bt_colarActionPerformed
    	
    	 selecionarAba();
    	
    	if( AbaSelec.getSelectedText() == null){
    		try{
    			AbaSelec.getStyledDocument().insertString(AbaSelec.getCaretPosition(), editar.colar(), null);
    		}catch(BadLocationException ble){
    		}
    	}else{
           AbaSelec.replaceSelection(editar.colar());
    	}
    		
    }//GEN-LAST:event_bt_colarActionPerformed

    private void bt_recortarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_recortarActionPerformed
    	//AbaSelec =(JTextPane)jTabbedPane1.getSelectedComponent();
    	
    	 selecionarAba();
    	 
    	if( AbaSelec.getSelectedText() != null){
    		editar.recortar( AbaSelec.getSelectedText());
    		 AbaSelec.replaceSelection("");
    		 
    	}
    }//GEN-LAST:event_bt_recortarActionPerformed

    
    private void bt_desfazerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_desfazerActionPerformed
    	
    	try {
    		undoList.get(jTabbedPane1.getSelectedIndex()).undo();
    	} catch (CannotRedoException cre) {
    		cre.printStackTrace();
    	}
    	updateButtons();
    }//GEN-LAST:event_bt_desfazerActionPerformed
    
    private void bt_refazerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_refazerActionPerformed
    	try {
    		undoList.get(jTabbedPane1.getSelectedIndex()).redo();
    	} catch (CannotRedoException cre) {
    		cre.printStackTrace();
    	}
    	updateButtons();
    }//GEN-LAST:event_bt_refazerActionPerformed

    private void sel_fonteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sel_fonteActionPerformed
        
    
    	selecionarAba();
        JComboBox cb = (JComboBox)evt.getSource();
        String tipo = (String)cb.getSelectedItem();
    	
    	if( AbaSelec.getSelectedText() != null){
    		formatar.tipoFonte(AbaSelec,tipo);
    	}
    	
    }//GEN-LAST:event_sel_fonteActionPerformed

    private void sel_tamanho_fonteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sel_tamanho_fonteActionPerformed
        
        //AbaSelec =(JTextPane)jTabbedPane1.getSelectedComponent();
    	selecionarAba();
        JComboBox cb = (JComboBox)evt.getSource();
        String tam = (String)cb.getSelectedItem();
    	
    	if( AbaSelec.getSelectedText() != null){
    		formatar.tamanho(AbaSelec,tam);
    	}
    	
    }//GEN-LAST:event_sel_tamanho_fonteActionPerformed
    
    private void sel_cor_fonteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sel_cor_fonteActionPerformed
        
        //AbaSelec =(JTextPane)jTabbedPane1.getSelectedComponent();
    	selecionarAba();
        JComboBox cb = (JComboBox)evt.getSource();
        String cor = (String)cb.getSelectedItem();
    	
    	if( AbaSelec.getSelectedText() != null){
    		formatar.corTexto(AbaSelec,cor);
    	}
    	
    }//GEN-LAST:event_sel_cor_fonteActionPerformed
    
    private void sel_esp_linhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sel_esp_linhaActionPerformed
        
    	selecionarAba();
        JComboBox cb = (JComboBox)evt.getSource();
        String esp = (String)cb.getSelectedItem();
    	
    	if( AbaSelec.getSelectedText() != null){
    		formatar.espLinha(AbaSelec,esp);
    	}
    	
    }//GEN-LAST:event_sel_esp_linhaActionPerformed

    private void bt_obter_ajudaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_obter_ajudaActionPerformed        
    	ajuda.obterAjuda();
    	
    }//GEN-LAST:event_bt_obter_ajudaActionPerformed

    private void bt_sobre_sistemaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_sobre_sistemaActionPerformed    	
    	ajuda.sobre();
    }//GEN-LAST:event_bt_sobre_sistemaActionPerformed

    private void bt_substituirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_substituirActionPerformed
        // TODO add your handling code here:
    
    	selecionarAba();
    	
    if( AbaSelec.getSelectedText() != null){
    	String de = AbaSelec.getSelectedText();
    	JOptionPane.showMessageDialog(null, de);
        String para = JOptionPane.showInputDialog("Para");
        
        int start = AbaSelec.getText().indexOf(de);
            if(start >= 0 && de.length() > 0)
            	AbaSelec.replaceSelection(para);
            
            if(start < 0)
                JOptionPane.showMessageDialog(null, "Palavra não encontrada no texto.");
            
            
    }  
    
 }//GEN-LAST:event_bt_substituirActionPerformed

   /* private void bt_destacar_sintaxeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_destacar_sintaxeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_bt_destacar_sintaxeActionPerformed
*/
    private void bt_busca_binariaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_busca_binariaActionPerformed
        // TODO add your handling code here:
    	 int posInicial = 0;
    	  selecionarAba();
    	
      
		 String pesquisa = JOptionPane.showInputDialog("Digite a palavra");
    	  
    	 if(jTabbedPane1.getSelectedIndex()>=0){
    		 
    		  if(pesquisa!=null){
				  
    			  String text = AbaSelec.getText(); 
    	    	 if(!text.equals("")){    
				  String stsPalaEncon =ferramenta.buscar(text,pesquisa);
				  
				  if(stsPalaEncon!=""){
				       int res = AbaSelec.getText().indexOf(stsPalaEncon, posInicial);	                   
		              
		                	   AbaSelec.requestFocus();
		                	   AbaSelec.select(res, res + stsPalaEncon.length());
		                       posInicial = res + stsPalaEncon.length();
		                 
				    }else{
				    	JOptionPane.showMessageDialog(null, "Palavra que contem substring não encontrado");
				    	
				    } 
	                 
				   }
    		 
    		 
    		  }
    		 
	        
    	  }
    }//GEN-LAST:event_bt_busca_binariaActionPerformed

    private void bt_busca_padraoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_busca_padraoActionPerformed
        // TODO add your handling code here:   	
				
		
    	
    	 selecionarAba();
   
    	 if(jTabbedPane1.getSelectedIndex()>=0){
    		int posInicial = 0;
				 String pesquisa = JOptionPane.showInputDialog("Digite a palavra");
	               //retorna o indice de uma string na primeira ocorrêcia de uma substring especificada
	               
				 
				  if(pesquisa!=null){
					  
			
					  
					  
				    int res = AbaSelec.getText().indexOf(pesquisa, posInicial);	                   
	                 if(res < 0){
	                     JOptionPane.showMessageDialog(null, "Texto não encontrado");
	                      posInicial = 0;   
	                 } 
	                 else{
	                	   AbaSelec.requestFocus();
	                	   AbaSelec.select(res, res + pesquisa.length());
	                       posInicial = res + pesquisa.length();
	                 }
	                 
	                 
				   }
	                 
    	 }
}
        
    
private int setLineNumber( int line) {
        int currentLine = 0;
        int currentSelection = 0;
       
        selecionarAba();
        
        String textContent = AbaSelec.getText();
        String seperator = System.getProperty("line.seperator");
        int seperatorLength = seperator.length();
        while (currentLine < line) {
            int next = textContent.indexOf(seperator,currentSelection);
            if (next > -1) {
                currentSelection = next + seperatorLength;
                currentLine++;
            } else {
                // set to the end of doc
                currentSelection = textContent.length();
                currentLine= line; // exits loop
            }
        }
        AbaSelec.setCaretPosition(currentSelection);
		return currentLine;
 }
    

    private void bt_num_linhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_num_linhaActionPerformed
        
    	
    	if(((JScrollPane)jTabbedPane1.getSelectedComponent()).getRowHeader().isVisible())
    		((JScrollPane)jTabbedPane1.getSelectedComponent()).getRowHeader().setVisible(false);
    	else
    		((JScrollPane)jTabbedPane1.getSelectedComponent()).getRowHeader().setVisible(true);
    
    	
    }//GEN-LAST:event_bt_num_linhaActionPerformed
    
    private void bt_corretor_ortograficoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_corretor_ortograficoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_bt_corretor_ortograficoActionPerformed

    private void bt_ordernar_alfabeticamenteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_ordernar_alfabeticamenteActionPerformed
        // TODO add your handling code here:
    	
    	 selecionarAba();
    	 JTextArea textOrd;
     	
     	//if( AbaSelec.getSelectedText() != null){
    	 if(jTabbedPane1.getSelectedIndex()>=0){
     		textOrd= ferramenta.ordenarAlfabeticamente(AbaSelec);
     		
     		AbaSelec.setText(null);
     		
     		AbaSelec.setText(textOrd.getText());
     	}
    	
    	
    	
    }//GEN-LAST:event_bt_ordernar_alfabeticamenteActionPerformed

    private void bt_justificadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_justificadoActionPerformed
        
    	
    	 selecionarAba();
    	
    	if( AbaSelec.getSelectedText() != null){
    		//formatar.justificar(jTextPane1.getSelectedText());
    		formatar.justificar(AbaSelec);
    	}
    }//GEN-LAST:event_bt_justificadoActionPerformed

    private void bt_centralizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_centralizarActionPerformed
    	
    	
    	 selecionarAba();
    	
    	if( AbaSelec.getSelectedText() != null){
    		formatar.centralizar(AbaSelec);
    	}
    }//GEN-LAST:event_bt_centralizarActionPerformed

    private void bt_direitaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_direitaActionPerformed
        
      
    	 selecionarAba();
        
        if( AbaSelec.getSelectedText() != null){
    		formatar.direita(AbaSelec);
    	}
    }//GEN-LAST:event_bt_direitaActionPerformed

    private void bt_esquerdaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_esquerdaActionPerformed
        
    	
    	 selecionarAba();
    	
    	if( AbaSelec.getSelectedText() != null){
    		formatar.esquerda(AbaSelec);
    	}
    }//GEN-LAST:event_bt_esquerdaActionPerformed

    private void bt_cabecalhoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_cabecalhoActionPerformed
        selecionarAba();
    	
    	if( AbaSelec.getSelectedText() != null){
    		formatar.cabecalho(AbaSelec);
    	}
    }//GEN-LAST:event_bt_cabecalhoActionPerformed

    private void bt_rodapeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_rodapeActionPerformed
        selecionarAba();
    	
    	if( AbaSelec.getSelectedText() != null){
    		formatar.rodape(AbaSelec);
    	}
    }//GEN-LAST:event_bt_rodapeActionPerformed

    private void bt_sublinhadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_sublinhadoActionPerformed
        
    
    	 selecionarAba();
    	if( AbaSelec.getSelectedText() != null){
    		formatar.sublinhado(AbaSelec);
    	}
    }//GEN-LAST:event_bt_sublinhadoActionPerformed

    private void bt_negritoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_negritoActionPerformed
        
    	 selecionarAba();
    	if( AbaSelec.getSelectedText() != null){
    		formatar.negrito(AbaSelec);
    	}
    }//GEN-LAST:event_bt_negritoActionPerformed

    private void bt_italicoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_italicoActionPerformed
       
    	 selecionarAba();
        if( AbaSelec.getSelectedText() != null){
    		formatar.italico(AbaSelec);
    	}
    }//GEN-LAST:event_bt_italicoActionPerformed

    
    public void updateButtons() {
    	
    	if(jTabbedPane1.getSelectedIndex()>=0){
          bt_desfazer.setText(undoList.get(jTabbedPane1.getSelectedIndex()).getUndoPresentationName());
          bt_refazer.setText(undoList.get(jTabbedPane1.getSelectedIndex()).getRedoPresentationName());
          bt_desfazer.setEnabled(undoList.get(jTabbedPane1.getSelectedIndex()).canUndo());
          bt_refazer.setEnabled(undoList.get(jTabbedPane1.getSelectedIndex()).canRedo());
        
    	}
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(EditorView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EditorView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EditorView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EditorView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EditorView().setVisible(true);
            }
        });
    }
    
    //Controller Variables

    ArquivoController arquivo = new  ArquivoController();
    EditarController editar = new EditarController();
    AjudaController ajuda = new AjudaController();
    FormatarController formatar = new FormatarController();
    FerramentasController ferramenta = new FerramentasController();

  
    
    //Auxiliar Variables
    List<UndoManager> undoList = new ArrayList<UndoManager>();
    private UndoManager undoManager = new UndoManager();
    FileDialog salvarArq;
    private int i;
    int num;
    JTextPane AbaSelec;
    JScrollPane novoScroll;
    
    
    //metodos auxiliares
    
    private void selecionarAba(){//Metodo para  aba selecionada
    	
     if(jTabbedPane1.getSelectedComponent()!=null)	{
  	   novoScroll =(JScrollPane)jTabbedPane1.getSelectedComponent();
        num = jTabbedPane1.indexOfComponent(novoScroll);
       JViewport vp = novoScroll.getViewport(); 
    
       if(vp ==null) return;
     	for(Component comp : vp.getComponents()){  
     	    if(comp instanceof JTextPane){  
     	    	AbaSelec = (JTextPane) comp;  
     	    	  
     	    }  
     	} 
     	 
     }
     	
     }
    
    
    private void selecionarAba2(int pos) {//metodo para selecionar aba de acordo com indice
 
    
    	   novoScroll =(JScrollPane)jTabbedPane1.getComponentAt(pos);
    	 
    	   JViewport vp = novoScroll.getViewport(); 
 		   
	    	for(Component comp : vp.getComponents()){  
	    	    if(comp instanceof JTextPane){  
	    	    	AbaSelec = (JTextPane) comp; 
	    	    	
	    	    	
	    	    	
	    	    }  
	    	}
    	   
    	
 		
    }
    
   
    
 private void fecharAbasUnico(){
    	
	
    	
    	 String nometab = null;
    	 selecionarAba();
    	   
    	 
    	 
    	if( jTabbedPane1.getSelectedComponent()!=null){
            nometab = jTabbedPane1.getTitleAt(jTabbedPane1.getSelectedIndex());
 	   
 	   
    	   int resposta=JOptionPane.showConfirmDialog(null, "Deseja salvar alteracoes em: "+ nometab+" ?");
    		
    	   if(resposta == JOptionPane.YES_OPTION){	
			      	   if(AbaSelec.getName()==null){
			      		 
			    		        salvarArq = new FileDialog(new Dialog(this), "Salvar", FileDialog.SAVE);
			    		       salvarArq.setVisible(true);
			              
				               if(salvarArq.getFile() !=null){
				                   jTabbedPane1.setTitleAt(num,salvarArq.getFile());
				                   arquivo.salvar(salvarArq.getDirectory()+salvarArq.getFile()+".txt",  AbaSelec.getText());
				                   AbaSelec.setName(salvarArq.getDirectory()+salvarArq.getFile()+".txt");
				               }
			    	     }else{
			    	    	 
			    	    	 arquivo.salvar(AbaSelec.getName(), AbaSelec.getText()); 
			    	     }
	      	   
    	    } else if(resposta == JOptionPane.CANCEL_OPTION){
	    	     	
	    	    	    return;
	        }
      	 
      	 
        if(num>=0){ 
    	  jTabbedPane1.removeTabAt(num);
    	
    	   arquivo.fechar();
       }
    }
    	
 }
 
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_abrir;
    private javax.swing.JButton bt_busca_binaria;
    private javax.swing.JButton bt_busca_padrao;
    private javax.swing.JButton bt_cabecalho;
    private javax.swing.JButton bt_centralizar;
    private javax.swing.JButton bt_colar;
    private javax.swing.JButton bt_copiar;
    private javax.swing.JButton bt_corretor_ortografico;
    private javax.swing.JButton bt_desfazer;
  //  private javax.swing.JButton bt_destacar_sintaxe;
    private javax.swing.JButton bt_direita;
    private javax.swing.JButton bt_esquerda;
    private javax.swing.JButton bt_fechar;
    private javax.swing.JButton bt_fechar_todos;
    private javax.swing.JButton bt_imprimir;
    private javax.swing.JButton bt_italico;
    private javax.swing.JButton bt_justificado;
    private javax.swing.JButton bt_negrito;
    private javax.swing.JButton bt_novo;
    private javax.swing.JButton bt_num_linha;
    private javax.swing.JButton bt_obter_ajuda;
    private javax.swing.JButton bt_ordernar_alfabeticamente;
    private javax.swing.JButton bt_propriedades;
    private javax.swing.JButton bt_recortar;
    private javax.swing.JButton bt_refazer;
    private javax.swing.JButton bt_rodape;
    private javax.swing.JButton bt_salvar;
    private javax.swing.JButton bt_salvar_como;
    private javax.swing.JButton bt_salvar_todos;
    private javax.swing.JButton bt_selecionar;
    private javax.swing.JButton bt_selecionar_tudo;
    private javax.swing.JButton bt_sobre_sistema;
    private javax.swing.JButton bt_sublinhado;
    private javax.swing.JButton bt_substituir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu11;
    private javax.swing.JMenu jMenu7;
    private javax.swing.JMenu jMenu8;
    private javax.swing.JMenu jMenu9;
    private javax.swing.JMenuItem jMenuItem22;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator4;
    private javax.swing.JToolBar.Separator jSeparator5;
    private javax.swing.JToolBar.Separator jSeparator6;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTabbedPane jTabbedPane4;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JToolBar jToolBar2;
    private javax.swing.JToolBar jToolBar3;
    private javax.swing.JToolBar jToolBar4;
    private javax.swing.JToolBar jToolBar5;
    private javax.swing.JToolBar jToolBar6;
    private javax.swing.JComboBox sel_fonte;
    private javax.swing.JComboBox sel_tamanho_fonte;
    private javax.swing.JComboBox sel_cor_fonte;
    private javax.swing.JComboBox sel_esp_linha;
    // End of variables declaration//GEN-END:variables
}